//
//  main.swift
//  Swift02
//
//  Created by 顾天宇 on 2017/9/13.
//  Copyright © 2017年 顾天宇. All rights reserved.
//

import Foundation
//(1)可选类型
//var s:String? = "Swift"  //可能有值可能没有值
//print(s)

//var f:String! = "Swift"
//print(f)

////(2)数组
//let arr1 = [2,3,4,5,6]//推断成int类型
//
//let arr2 = [2,3,4,5,3.2]//推断成doble类型
//
//var arr3 = [2,3,4,5,3.2,"swift"] as [Any]//推断成any类型
// arr3[1] = 100
// arr3.append(200)
// print(arr3)
//
////空数组
// var arr = [Any]()
// arr.append(1)
// arr.append("swift")
// print(arr)
//
//
////(3)字典
//var dic = [String:Any]()
// dic["language"] = "java"
// dic["age"] = 100
// dic.removeAtIndex(dic.startIndex)
// dic["language"] = "java"
//
//var dic2 = [1:100,2:200]
//print(dic2[1]!)

//(4)函数
//func print0_100(){
//    for i in 0...100{
//        print(i)
//    }
//}
//
////自文档语言
//func add(a:Int,b:Int) -> Int{
//    return(a+b)
//}
//
//print(add(a: 10, b: 20))



//(5)枚举
//enum Direction:String{
//    case Up = "Up"
//    case Right = "Right"
//    case Down = "Down"
//    case Left = "Left"
//    
//    //枚举中可以定义方法
//    func getRotation() -> Int {
//        switch self {
//        case .Up:
//            return 10
//        default:
//            return 200
//        }
//    }
//
//
//    print(Direction.Up)
//    print(Direction.Up.hashValue)
//    print(Direction.Up.rawValue)
//    print(Direction.Up.getRotation()}




////(6)结构体
//    struct Student {
//    var name:String
//    var age:Int
//    
//    init(name:String,age:Int) {
//    self.name = name
//    self.age = age
//    }
//    func toString() -> String {
//    return "年龄：\(self.age),名字:\(self.name)"
//    }
//}
//
//var s = Student()
//print(s.age)
//print(s.toString)







//作业二：构造10个学生（要求有学生的姓名、数学成绩、英语成绩），
//按照学生英语和数学平均分的成绩从小到大输出学生的姓名、数学成绩、英语成绩及平均分。



struct Student{
    var name:String
    var mathScore:Int
    var englishScore:Int
    var average:Float

    init(name:String,mathScore:Int,englishScore:Int) {
        self.name = name
        self.mathScore = mathScore
        self.englishScore = englishScore
        self.average = Float(self.mathScore + self.englishScore) / 2
    }
}





func sortWithAverage(students:[Student]) -> [Student]{
    var result = students
    for i in 0...result.count-2 {
        for j in i+1...result.count-1{
            if(result[i].average > result[j].average){
                let temp = result[i]
                result[i] = result[j]
                result[j] = temp
            }
            }
    }
    return result
}

//构造10个学生对象
var student01 = Student(name: "顾天宇1", mathScore: 100, englishScore: 100)
var student02 = Student(name: "顾天宇2", mathScore: 98, englishScore: 99)
var student03 = Student(name: "顾天宇3", mathScore: 88, englishScore: 86)
var student04 = Student(name: "顾天宇4", mathScore: 75, englishScore: 88)
var student05 = Student(name: "顾天宇5", mathScore: 95, englishScore: 97)
var student06 = Student(name: "顾天宇6", mathScore: 85, englishScore: 94)
var student07 = Student(name: "顾天宇7", mathScore: 92, englishScore: 66)
var student08 = Student(name: "顾天宇8", mathScore: 63, englishScore: 66)
var student09 = Student(name: "顾天宇9", mathScore: 63, englishScore: 99)
var student10 = Student(name: "顾天宇10", mathScore: 99, englishScore: 12)



var result = sortWithAverage(students: [student01,
                                        student02,
                                        student03,
                                        student04,
                                        student05,
                                        student06,
                                        student07,
                                        student08,
                                        student09,
                                        student10])

print("英语和数学平均分的成绩从小到大排列为:")
//遍历结果数组并打印
for i in 0..<result.count{
    print("姓名:\(result[i].name),数学成绩:\(result[i].mathScore),英语成绩:\(result[i].englishScore),平均分:\(result[i].average)")
}
















